/*
 * Copyright (c) 2007, Niek Sanders
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Rochester Institute of Technology nor the names
 *       of its contributors may be used to endorse or promote products derived
 *       from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 */

/**
 * \class DE405EphemerisIO
 *
 * A class to read binary JPL DE405 data.  This class reads the output file 
 * generated by util/convert.cpp.
 *
 * It expects the following data layout:
 *
 * 13 integers indicating coefficients offsets (one per astro entity).
 * 13 integers indicating chebyshev poly degree (one per astro entity).
 * 13 integers indicating record subdivisions (one per astro entity).
 * 2 doubles, indicating the start and end julian day of coverage
 * doubles representing chebyshev coefficients (same ordering as ASCII data).
 *
 * Obviously, convert.cpp needs to be compiled/run on the same platform as this 
 * IO class for things like endianness to work out correctly.
 *
 * Currently, this class leaves the coefficient data in the file, reading as 
 * needed.
 */
#ifndef DE405EPHEMERISIO_H
#define DE405EPHEMERISIO_H

#include <fstream>
#include <stdexcept>
#include <string>

#include "DE405Ephemeris.h"

class DE405EphemerisIO {

    private:
        // File used to pull coefficients and layout information
        std::string dataFilename_;

        // File stream used for manipulating the ephemerides
        mutable std::ifstream dataStream_;

        // Offsets for each astro entity with a given record
        int coeffOffsets_[ 13 ];

        // Number coefficients for each astro entity
        int numCoeffs_[ 13 ];

        // Number coefficient-sets per record for each astro entity 
        int numCoeffSets_[ 13 ];

        // Start and end day of the coverage
        double startDay_;
        double endDay_;

        // Total number coefficients per record 
        int coeffPerRecord_;

        // Number of days covered by a record
        const static double intervalDuration_;


    public:
        DE405EphemerisIO( const std::string& dataFilename );

        void getInterpolationInfo( double julianDay, 
                                   DE405Ephemeris::AstroEntity entity, 
                                   std::vector<double>& coeffs, 
                                   double& setsPerDay,
                                   double& chebyshevTime ) const;

    private:
        // disable default ctor
        DE405EphemerisIO( void );

};

#endif // DE405EPHEMERISIO_H
